package com.youku.controls
{
   import com.youku.plugins.commonui.CommonUIFacade;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import com.youku.events.PluginEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.PlayerConfig;
   import com.youku.conductor.ConductorState;
   
   public class CommonUIController extends BasePluginController
   {
      
      public function CommonUIController() {
         super();
         this.init();
      }
      
      private var _commonUIFacade:CommonUIFacade;
      
      override protected function init() : void {
         this._commonUIFacade = CommonUIFacade.getInstance();
         this._commonUIFacade.init(viewManager,playerProxy);
         GlobalService.addService(GlobalServiceKeys.COMMON_UI_FACADE,this._commonUIFacade);
         playerProxy.addEventListener(PluginEvent.VIDEO_LAYER_TOGGLE_PLAYPAUSE,this.onCommonUIEvent);
         playerProxy.addEventListener(PluginEvent.VIDEO_LAYER_TOGGLE_FULLSCREEN,this.onCommonUIEvent);
         playerProxy.addEventListener(PluginEvent.VIDEO_LAYER_CLOSE_FULLSCREEN,this.onCommonUIEvent);
         playerProxy.addEventListener(PluginEvent.POST_VIDEO_SCREEN_COMPLETE,this.onCommonUIEvent);
         playerProxy.addEventListener(PluginEvent.PRE_VIDEO_SCREEN_COMPLETE,this.onCommonUIEvent);
         playerProxy.addEventListener(PluginEvent.READY_TO_PLAY_CLICK,this.onCommonUIEvent);
      }
      
      private function onCommonUIEvent(param1:PluginEvent) : void {
         switch(param1.type)
         {
            case PluginEvent.POST_VIDEO_SCREEN_COMPLETE:
               playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.HIDE_POST_VIDEO_SCREEN));
               conductor.postVideoDone();
               break;
            case PluginEvent.PRE_VIDEO_SCREEN_COMPLETE:
               playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.HIDE_PRE_VIDEO_SCREEN));
               conductor.preVideoDone();
               break;
            case PluginEvent.VIDEO_LAYER_CLOSE_FULLSCREEN:
               if(PlayerConfig.isFullScreen)
               {
                  changeFullScreenMode();
               }
               break;
            case PluginEvent.VIDEO_LAYER_TOGGLE_FULLSCREEN:
               if(!isPartnerAllowFullScreen)
               {
                  return;
               }
               changeFullScreenMode();
               break;
            case PluginEvent.VIDEO_LAYER_TOGGLE_PLAYPAUSE:
               if(conductor.currentState == ConductorState.VIDEO)
               {
                  playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.CORE_TOGGLE_PAUSEPLAY,{"useraction":true}));
               }
               break;
            case PluginEvent.READY_TO_PLAY_CLICK:
               conductor.userPlay();
               break;
         }
      }
   }
}
